﻿@model Retete.ViewModels.ReceptieViewModel
@{
    ViewBag.Title = "Receptii";
}

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui.min.js")" type="text/javascript"></script>

<script type="text/javascript">
    window.onload = function () {
        if (AdaugaProdusInventar.value == "True") {
            spanAdaugareProdus.setAttribute("style", "display: block");
            AdaugaProdusInventar.value = false;
        }
        else spanAdaugareProdus.setAttribute("style", "display: none");

        if (FocusProdusNou.value == "True") ProdusNouNume.focus();
        else Receptie_FurnizorNume.focus();
    }

    function submitOnEnter() {
        if (event.keyCode == "13") form.submit();
    }
    function refreshOnEnter() {
        if (event.keyCode == "13") {
            Refresh.value = true;
            form.submit();
        }
    }
    function stergeInregistrare(id) {
        if (confirm("Doriti sa stergeti produsul selectat?") == true) {
            IDProdusSters.value = id;
            form.submit();
        }
    }
    function adaugaProdusInventar() {
        AdaugaProdusInventar.value = true;
        form.submit();
    }
</script>

<script type="text/javascript">
    $(function () {
        $("#Receptie_Tva").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: '@Url.Content("~/Autocomplete/Tva")',
                    dataType: "json",
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { value: item.Valoare }
                        }));
                    }
                });
            },
            minLength: 0,
            delay: 50
        });
    });

    $(function () {
        $("#Receptie_FurnizorNume").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: '@Url.Content("~/Autocomplete/Furnizori")',
                    dataType: "json",
                    data: { maxRows: 8, contains: request.term },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { value: item.Nume, adresa: item.Adresa, banca: item.Banca, codFiscal: item.CodFiscal, codRegistruComert: item.CodRegistruComert, cont: item.Cont, judet: item.Judet }
                        }));
                    }
                });
            },
            minLength: 2,
            delay: 50,
            select: function (event, ui) {
                Receptie_FurnizorAdresa.value = ui.item.adresa;
                Receptie_FurnizorJudet.value = ui.item.judet;
                Receptie_FurnizorCodFiscal.value = ui.item.codFiscal;
                Receptie_FurnizorCodRegistruComert.value = ui.item.codRegistruComert;
                Receptie_FurnizorCont.value = ui.item.cont;
                Receptie_FurnizorBanca.value = ui.item.banca;
            }
        });
    });

    $(function () {
        $("#ProdusNouNume").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: '@Url.Content("~/Autocomplete/Produse")',
                    dataType: "json",
                    data: { maxRows: 8, contains: request.term },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { value: item.Cod + ' ' + item.Nume, id: item.ID, nume: item.Nume, unitate: item.UnitateMasura, cod: item.Cod }
                        }));
                    }
                });
            },
            minLength: 2,
            delay: 50,
            select: function (event, ui) {
                UnitateMasura.innerText = ui.item.unitate;
                ProdusNouID.value = ui.item.id;
                ProdusNou_Cod.innerText = ui.item.cod;
                ProdusNouCod.value = ui.item.cod;
                ProdusNouNume.value = ui.item.nume;
            }
        });
    });

    $(function () {
        $("#Receptie_Data").datepicker({ dateFormat: 'mm/dd/yy' });
    });
</script>

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "form", name = "form" }))
{
    <table style="width: 100%">
        <tr>
            <td style="width: 190px; vertical-align: top; border-right-style: dashed; border-right-color: #CFCFCF; border-right-width: 1px;">
                <fieldset>
                    <legend>Activitati</legend>

                    <p class="toolbox"><a href="javascript:form.submit()">Accepta</a></p>
                    <p class="toolbox">@Html.ActionLink("Renunta", "Index")</p>
                </fieldset>
            </td>
            <td style="vertical-align: top;">
                @Html.ValidationSummary(false)
                @Html.HiddenFor(model => model.IDProdusSters)
                @Html.HiddenFor(model => model.FocusProdusNou)
                @Html.HiddenFor(model => model.AdaugaProdusInventar)
                @Html.HiddenFor(model => model.CodUltimulProdus)

                <fieldset>
                    <legend>Detalii receptie</legend>

                    <p>
                        @Html.LabelFor(model => model.Receptie.Numar, "Numar si data")
                        @Html.TextBoxFor(model => model.Receptie.Numar, new { @style = "width: 145px" })
                        @Html.TextBoxFor(model => model.Receptie.Data, new { @style = "width: 146px" })
                    </p>

                    <p>
                        @Html.LabelFor(model => model.Receptie.TipDocumentLivrare, "Tip si numar livrare")
                        @Html.DropDownListFor(model => model.Receptie.TipDocumentLivrare, new SelectList(new string[] { "Factura", "Borderou achizitie" }), new { @style = "width: 151px" })
                        @Html.TextBoxFor(model => model.Receptie.NumarDocumentLivrare, new { @style = "width: 146px" })
                    </p>

                    <p>
                        @Html.LabelFor(model => model.Receptie.Tva, "TVA")
                        @Html.TextBoxFor(model => model.Receptie.Tva)
                    </p>
                </fieldset>
    
                <fieldset>
                    <legend>Detalii furnizor</legend>

                    <p>
                        @Html.LabelFor(model => model.Receptie.FurnizorNume, "Furnizor")
                        @Html.EditorFor(model => model.Receptie.FurnizorNume)
                    </p>

                    <p>
                        @Html.LabelFor(model => model.Receptie.FurnizorAdresa, "Adresa si judet")
                        @Html.TextBoxFor(model => model.Receptie.FurnizorAdresa, new { style = "width: 200px" })
                        @Html.TextBoxFor(model => model.Receptie.FurnizorJudet, new { style = "width: 91px" })
                    </p>
        
                    <p>
                        @Html.LabelFor(model => model.Receptie.FurnizorCodFiscal, "CUI si cod reg. comert")
                        @Html.TextBoxFor(model => model.Receptie.FurnizorCodFiscal, new { style = "width: 145px" })
                        @Html.TextBoxFor(model => model.Receptie.FurnizorCodRegistruComert, new { style = "width: 146px" })
                    </p>

                    <p>
                        @Html.LabelFor(model => model.Receptie.FurnizorCont, "Cont si banca")
                        @Html.TextBoxFor(model => model.Receptie.FurnizorCont, new { style = "width: 145px" })
                        @Html.TextBoxFor(model => model.Receptie.FurnizorBanca, new { style = "width: 146px" })
                    </p>
                </fieldset>

                <fieldset>
                    <legend>Produse cumparate (pret de achizitie fara TVA, pret de vanzare cu TVA)</legend>
            
                    <span id="spanAdaugareProdus">
                        Produsul selectat nu exista (sau nu l-ati selectat corect - foarte probabil). 
                        <a href="javascript:adaugaProdusInventar()">Adauga produsul in inventar</a>
                    </span>

                    <table class="grid">
                        <thead>
                            <tr>
                                <th style="width: 16px"></th>
                                <th style="width: 70px">Cod</th>
                                <th>Produs</th>
                                <th style="width: 60px">Gramaj</th>
                                <th style="width: 40px">Cant.</th>
                                <th style="width: 40px">UM</th>
                                <th style="width: 40px">TVA</th>
                                <th style="width: 70px">Cumparare</th>
                                <th style="width: 70px">Valoare</th>
                                <th style="width: 70px">Val. TVA</th>
                                <th style="width: 70px">Val. cu TVA</th>
                                <th style="width: 70px">Vanzare</th>
                            </tr>
                        </thead>
        
                        <tbody id="tableBody">
                            <tr class="insertionRow">
                                <td></td>
                                <td id="ProdusNou_Cod"></td>
                                <td>
                                    @Html.TextBoxFor(model => model.ProdusNouNume, new { autocomplete = "off" })
                                    @Html.HiddenFor(model => model.ProdusNouID)
                                    @Html.HiddenFor(model => model.ProdusNouCod)
                                </td>
                                <td>@Html.TextBoxFor(model => model.ProdusNouGramaj, new { autocomplete = "off" })</td>
                                <td>@Html.TextBoxFor(model => model.ProdusNouCantitate, new { autocomplete = "off" })</td>
                                <td id="UnitateMasura"></td>
                                <td>@Html.CheckBoxFor(model => model.ProdusNouTVA, new { style = "height: 13px;" })</td>
                                <td>@Html.TextBoxFor(model => model.ProdusNouPret, new { autocomplete = "off" })</td>
                                <td></td>
                                <td></td>
                                <td id="ValoareCuTva"></td>
                                <td>@Html.TextBoxFor(model => model.ProdusNouPretVanzare, new { autocomplete = "off", onkeydown = "return submitOnEnter()" })</td>
                            </tr>

                            @if (@Model.Produse != null)
                            {
                                for (int i = 0; i < Model.Produse.Count; i++)
                                {
                                    <tr>
                                        <td>
                                            <img src="@Url.Content("~/Content/Images/Delete.png")" alt="Sterge" style="height: 12px; cursor: pointer;" onclick="stergeInregistrare(@Model.Produse[i].ID)" />
                                            @Html.HiddenFor(element => element.Produse[i].ID)
                                            @Html.HiddenFor(element => element.Produse[i].Receptie.Tva)
                                        </td>
                                        <td>
                                            @Html.DisplayTextFor(element => element.Produse[i].Produs.Cod)
                                            @Html.HiddenFor(element => element.Produse[i].Produs.Cod)
                                            @Html.HiddenFor(element => element.Produse[i].Produs.Categorie)
                                        </td>
                                        <td>
                                            @Html.DisplayTextFor(element => element.Produse[i].Produs.Nume)
                                            @Html.HiddenFor(element => element.Produse[i].Produs.Nume)
                                            @Html.HiddenFor(element => element.Produse[i].IDProdus)
                                        </td>
                                        <td>
                                            @Model.Produse[i].Gramaj.ToString("0.00")
                                            @Html.HiddenFor(element => element.Produse[i].Gramaj)
                                        </td>
                                        <td class="insertionCell">@Html.TextBoxFor(element => element.Produse[i].Cantitate)</td>
                                        <td>
                                            @Html.DisplayTextFor(element => element.Produse[i].Produs.UnitateMasura)
                                            @Html.HiddenFor(element => element.Produse[i].Produs.UnitateMasura)
                                        </td>
                                        <td class="checkBoxCell">@Html.CheckBoxFor(element => element.Produse[i].AdmiteTVA, new { style = "width: 20px;" })</td>
                                        <td class="insertionCell">@Html.TextBoxFor(element => element.Produse[i].PretCumparare)</td>
                                        <td>
                                            @Model.Produse[i].Valoare.ToString("0.00")
                                            @Html.HiddenFor(element => element.Produse[i].Valoare)
                                        </td>
                                        <td>
                                            @Model.Produse[i].ValoareTVA.ToString("0.00")
                                            @Html.HiddenFor(element => element.Produse[i].ValoareTVA)
                                        </td>
                                        <td>
                                            @Model.Produse[i].ValoareCuTVA.ToString("0.00")
                                            @Html.HiddenFor(element => element.Produse[i].ValoareCuTVA)
                                        </td>
                                        <td class="insertionCell">@Html.TextBoxFor(element => element.Produse[i].PretVanzare)</td>
                                    </tr>
                                }
                                
                                <tr class="totals">
                                    <td></td>
                                    <td></td>
                                    <td>Total:</td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td class="insertionCell">@Html.TextBoxFor(element => element.TotalValoare)</td>
                                    <td class="insertionCell">@Html.TextBoxFor(element => element.TotalValoareTVA)</td>
                                    <td class="insertionCell">@Html.TextBoxFor(element => element.TotalValoareCuTVA)</td>
                                    <td></td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </fieldset>
            </td>
        </tr>
    </table>
}